package learnonline.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import learnonline.dao.RowMapper;
import learnonline.dao.UserDao;
import learnonline.entity.User;

public class UserDaoImpl implements UserDao{
	private JdbcImpl jdbc=new JdbcImpl();
	@Override
	public User checkUser(String userName) {
		String sql="select * from User where username=?";
		List<User> user=jdbc.queryList(sql,new RowMapper<User>(){
			@Override
			public User mapRow(ResultSet rs) throws SQLException {
				User user=new User();
				user.setUserName(rs.getString("userName"));
				user.setPaw(rs.getString("password"));
				user.setSname(rs.getString("sname"));
				user.setAuthority(rs.getInt("authority"));
				return user;
			}
			
		},userName);
		return user.size()>0?user.get(0):null;
	}
	@Override
	public User UserConfirm(String userName, String psw) {
		String sql="select * from user where username=? and password=?";
		List<User> user=jdbc.queryList(sql,new RowMapper<User>(){
			@Override
			public User mapRow(ResultSet rs) throws SQLException {
				User user=new User();
				user.setUserName(rs.getString("userName"));
				user.setPaw(rs.getString("password"));
				user.setSname(rs.getString("sname"));
				return user;
			}
			
		},userName,psw);
		return user.size()>0?user.get(0):null;
	}
	@Override
	public void register(String userName, String psw, String sname,int authority) {
		String sql="insert into user(username,password,sname,authority) value(?,?,?,?)";
		jdbc.executeUpdate(sql, userName,psw,sname,authority);
		
	}
	@Override
	public User checkNick(String nickName) {
		String sql="select * from user where sname=?";
		List<User> user=jdbc.queryList(sql,new RowMapper<User>(){
			@Override
			public User mapRow(ResultSet rs) throws SQLException {
				User user=new User();
				user.setUserName(rs.getString("userName"));
				user.setPaw(rs.getString("passWord"));
				user.setSname(rs.getString("sname"));
				return user;
			}
			
		},nickName);
		return user.size()>0?user.get(0):null;
	}
	
}
